package com.and9.mchen.mcmusic.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.dao.IBaseEntityDao;
import com.and9.mchen.mcmusic.entity.BaseEntity;
import com.google.gson.Gson;

/**
 * 
 * @author MC
 * @version  1.0
 */
public final class EntityHelper {
	
	private final static Log log = LogFactory.getLog(EntityHelper.class);
	
	private EntityHelper(){}
	
	/**
	 * return a string value of uniqueId ( thread-safe )
	 * @return
	 */
	public static synchronized String createUniqueId(){
		
		return UUID.randomUUID().toString();
		
	}
	
	public static boolean containBaseEntityPropertyName(String... propertyNames){
		
		String[] array=IBaseEntityDao.BASEPROPERTYNAMES;
		
		for(int i=0; i<array.length; i++){
			for(int j=0; j<propertyNames.length; j++){
				if(array[i].equals(propertyNames[j])){
					return true;
				}
			}
		}
		
		return false;
	}
	
	
	public static String[] groupArrays(String[]... stringArrays){
		
		List<String> list=new LinkedList<String>();
		
		for(String[] strings : stringArrays){
			for(String string : strings){
				list.add(string);
			}
		}
		
		if(GEntityHelper.isEmpty(list)){
			return null;
		}
	
		String[] strings=new String[list.size()];
		
		int i=0;
		
		for(String string : list){
			strings[i++]=string;
		}
		
		return strings;
		
	}
	
	public static Object[] groupArrays(Object[]... objectArrays){
		
		List<Object> list=new ArrayList<Object>();
		
		for(Object[] objects : objectArrays){
			
			if(!GEntityHelper.isEmpty(objects)){
				for(Object object : objects){
					list.add(object);
				}
			}
		}
		
		return list.toArray();
		
	}
	
	/**
	 * <p>
	 * list covert to set(HashSet)<br/>
	 * if list is null or empty<br/>
	 * return a empty set
	 * </p>
	 * @param list
	 * @return
	 */
	public static <T> Set<T> listConvertToSet(List<T> list){
		
		Set<T> set = new HashSet<T>();
		
		for(T object : list){
			set.add(object);
		}
		
		return set;
	}
	
	
	public static <T> List<T> setConvertToList(Set<T> set){
		
		List<T> list=new LinkedList<T>();
		
		for(T object : set){
			list.add(object);
		}
		
		return list;
	}
	
	public static Timestamp getTimestampByDate(int year, int month, int date){
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DATE, date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
        return new Timestamp(calendar.getTimeInMillis());
	}
	
	public static Object[][] initRelevanceParamsById(BaseEntity entity, Set<? extends BaseEntity> baseEntities) {
		
		Object[][] params=new Object[baseEntities.size()][2];
		
		int i=0;
		
		for(BaseEntity baseEntity : baseEntities){
			params[i][0]=entity.getId();
			params[i][1]=baseEntity.getId();
			i++;
		}
		return params;
	}
	
	public static Object createDaoByProperty(String daoKey){
		
		Properties prop =PropertyUtil.getProperties("daoConfig.properties");
		
		if(GEntityHelper.isEmpty(prop) || GEntityHelper.isEmpty(daoKey)){
			return null;
		}
		
		try {
			String daoClass = prop.getProperty(daoKey);
			return Class.forName(daoClass).newInstance();	
		} catch (Exception e) {
			log.error("load dao property failure", e);
			return null;
		}
	}
	
	public static Object createServiceByProperty(String serviceKey){
		
		Properties prop =PropertyUtil.getProperties("serviceConfig.properties");
		
		if(GEntityHelper.isEmpty(prop) || GEntityHelper.isEmpty(serviceKey)){
			return null;
		}
		
		try {
			String serviceClass = prop.getProperty(serviceKey);
			return Class.forName(serviceClass).newInstance();	
		} catch (Exception e) {
			log.error("load service property failure", e);
			return null;
		}
	}
	
	public static <T> String covertToJson(T object){
		Gson gson=new Gson();
		return gson.toJson(object);
	}
	
}
